function [ EC ] = APEcombinedatasets( all )
% [ EC ] = APEcombinedatasets( all )
%
%   Allows for semi-interactive combination of APE datasets into a single
%   file.
%       ATTENTION:
%       Not robust! User has to make sure beforehand that all datafiles
%       intended to be combined have the same X- and Y-content and -scale !!

%   INPUT       all     :   1 = all data will be combined
%   OUTPUT      EC      :   Error Code (0 = OK, 1 = Error)

%  (c) F. Dworkowski, 2011
%      Version 1.1, 2012-02-21

%% ===== CONFIG & INITILIZE =====
    if nargin < 1
        all = 0;
    end
    cont = 1;
    doBatch = 0;
    i = 1;
    ytypeanswer = 0;
    
    initialWarning = {'ATTENTION:'; ...
                      ''; ...
                      'Not robust!'; ...
                      ''; ...
                      'User has to make sure beforehand'; ...
                      'that all datafiles intended to be combined'; ...
                      'have the same X- and Y-content and -scale !!';...
                      ''};

%% ===== CODE =====
warn = warndlg(initialWarning,'Warning','modal');
    uiwait(warn)
    
while cont == 1         % START WHILE --------------------------------------
% ----- Choose Folder and detect files -----
    if doBatch == 1
        FileName = filelist(i).name;
    elseif doBatch == 0
        [FileName,PathName,FilterIndex] = uigetfile({'*.mat';'*.*'},'File Selector');
        if isequal(FileName,0)
           disp('User selected Cancel')
           EC = 1;
           return
        else
           disp(['User selected ', fullfile(PathName, FileName)])
        end
    % Detect all available .mat files as preparation for possible doBatch
        filelist=dir('*.mat');      
    else
        errordlg('Problem with "doBatch"');
    end

% ----- load and translocate data -----
    load(fullfile(PathName, FileName))
    dataout.combinedFiles(i) = {FileName};
    if all ~= 0
        first.footer = footer;
    end
    if exist('dose','var')
        dataout.dose(i) = dose;
    end

% ----- very first datset -----
    if i == 1
        % ----- construct and keep output filename -----
        [pathstr, name, ext] = fileparts(FileName);
        outfile = [name,'-combinedset',ext];
        dataout.footer = footer;    % add footer of first file to output
        dataout.datax = datax;      % add datax of first file to output
        
        % ----- check and choose availabe X data -----
        if exist('ramandatax')
            if all == 0
            xtypeanswer = questdlg({'Which type of X data do you' ...
                'want to combine?'}, ...
                'DataX type','Raw','Ramanshift','Cancel','Ramanshift');
            else
                dataout.ramandatax = ramandatax;
                dataout.Laser_exwavelength = Laser_exwavelength;
            end
        elseif all == 0
            xtypeanswer = 'Raw';
        else
            errordlg('ERROR: no RAW data??')
        end 
        if all == 0
            switch xtypeanswer
                case 'Cancel'
                    EC = 1;
                    return
                case 'Raw'
                    newdatax = datax;
                case 'Ramanshift'
                    newdatax = ramandatax;
                    dataout.Laser_exwavelength = Laser_exwavelength;
            end
        end
    end
    
% ----- check for contained types of Y data -----
    if exist('ALSdatay','var')
        if all == 0
            if ytypeanswer == 0       % second part makes first choice universal
                ytypeanswer = questdlg({'Which type of Y data do you' ...
                            'want to combine?'}, ...
                            'DataY type','Raw','ALScor.','Cancel','Raw');
            end
        else 
            first.ALSpara = ALSpara;
        end
    elseif all == 0 
        if ytypeanswer == 0
            ytypeanswer = 'Raw';
        end
    end
    if all == 0
        switch ytypeanswer
            case 'Cancel'
            case 'Raw'
                newdatay(:,i) = datay(:,1);
            case 'ALScor.'
                newdatay(:,i) = ALSdatay(:,1);
        end
    end
    if all == 1
        dataout.datay(:,i) = datay(:,1);
        if exist('ALSdatay','var')
            dataout.ALSdatay(:,i) = ALSdatay(:,1);
            dataout.ALSbasey(:,i) = ALSbasey(:,1);
            dataout.ALSres(:,i) = ALSres(:,1);
        end
    end
        
% ----- Ask for additional data -----
    if doBatch ~= 1
        qanswer = questdlg({'Do you want to add another file?' ...
                               '' 'ATTENTION:' '' ...
                                ' all data must have the same x-axis!'}, ...
                                'Add file','Yes','No','Batch Folder','Yes');
                switch qanswer
                    case 'Cancel'
                        return
                    case 'Yes'
                        cont = 1;
                        doBatch = 0;
                    case 'No'
                        cont = 0;
                    case 'Batch Folder'
                        cont = 1;
                        doBatch = 1;
                end
    end
    % check for end of batch job
    if i >= length(filelist)
        cont = 0;
    else
        i = i+1;
    end
    clear datax
    clear datay    
end                     % END WHILE ----------------------------------------

% --- Construct Output File ---
if all == 0
    switch xtypeanswer
        case 'Cancel'
            EC = 1;
            return
        case 'Raw'
            dataout.datax = newdatax;
        case 'Ramanshift'
            dataout.ramandatax = newdatax;
    end

    switch ytypeanswer
        case 'Raw'
            dataout.datay = newdatay;
        case 'ALScor.'
            dataout.ALSdatay = newdatay;
    end
else
    if exist('ALSdatay','var')
        dataout.ALSpara = first.ALSpara;
    end
    dataout.footer  = first.footer;
end
save(fullfile(PathName, outfile), '-struct', 'dataout')
EC = 0;
clear
end
                